/*
 * @lc app=leetcode.cn id=90 lang=cpp
 *
 * [90] 子集 II
 */

// @lc code=start
class Solution
{
public:
  set<vector<int>> sets;
  int n;

  vector<vector<int>> subsetsWithDup(vector<int>& nums)
  {
    sort(nums.begin(), nums.end());
    this->n = nums.size();
    vector<int> tmp;
    Backtracking(nums, 0, tmp);
    return vector<vector<int>>(sets.begin(), sets.end());
  }

  void Backtracking(vector<int>& nums, int idx, vector<int>& tmp)
  {
    if (idx <= n) {
      sets.insert(tmp);
      for (int i = idx; i < n; ++i) {
        tmp.push_back(nums[i]);
        Backtracking(nums, i + 1, tmp);
        tmp.pop_back();
      }
    }
  }
};
// @lc code=end
